// pages/jinbi_qiandao/jinbi_qiandao.js
var util = require('../../utils/util.js')
var app = getApp()
let zhi = null
let openid = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yuanshengAd1:'adunit-df84d2891075f223',
    nowDate: '2021',
    from: 'qiandao',
    qiandaoNum: 1,
    yaoqingNum: 10,
    shareImg: "https://6d61-main-9g11cspaf548a298-1306430077.tcb.qcloud.la/cloudbase-cms/upload/2021-09-11/ly5ao1bg73md7xii9wv1v8jfflrznlc9_.jpg?sign=cbf7c166158e1b91d9f7cad5b3db0839&t=1631355785"
  },
  // 异步获取用户openid
  async getOpenIdsync() {
    return await wx.cloud.callFunction({
      name: 'user',
      data: {
        tag: 'getOpenId'
      }
    }).then(res => {
      return res.result
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLookVideos_qiandao() //  1.查询今天剩余签到次数
    this.getController() //异步获取系统控制变量
    this.setData({
      from: options.id, // 获取事件来源
      nowDate: util.formatTime(new Date()).slice(0, 10), // 设置今天的时间显示到页面
    })

  },
  //异步获取系统控制变量
  getController() {
    if (app.globalData.SysController == null) {
      app.getController().then(res => {
        zhi = res.jinbi_zhi.qiandao
        this.setData({
          yaoqingNum: res.jinbi_zhi.yaoqing,
          shareImg: res.jinbi_page_top_show.jinbiShareLgImg,
          yuanshengAd1:res.ad.yuansheng1
        })
      })
    } else {
      zhi = app.globalData.SysController.jinbi_zhi.qiandao
      this.setData({
        yaoqingNum: app.globalData.SysController.jinbi_zhi.yaoqing,
        shareImg: app.globalData.SysController.jinbi_page_top_show.jinbiShareLgImg,
        yuanshengAd1:app.globalData.SysController.ad.yuansheng1

      })
    }
  },
  onShow(options) {},
  // 定义查询方法
  dbLookVideosNum(_openid) {
    wx.cloud.database().collection('lookvideos_qiandao').where({
        _openid
      }).get()
      .then(res => {
        this.setData({
          qiandaoNum: res.data[0].qiandaoNum
        })
      })
  },
  //  获取观看和签到剩余次数及时间
  getLookVideos_qiandao() {
    if (!app.globalData.userInfo) {
      this.getOpenIdsync().then(res => {
        this.dbLookVideosNum(res)
        openid = res
      })
    } else {
      this.dbLookVideosNum(app.globalData.userInfo._openid)
      openid = app.globalData.userInfo._openid
    }
  },
  // 2.积分+1
  // 3.签到次数置0
  // 签到
  yesQiandao(e) {
    // console.log(app.globalData.gotoAppid.appid,'=============');
    if (e.currentTarget.dataset.tag == 'back') {
      wx.navigateToMiniProgram({
        appId: app.globalData.gotoAppid.appid,
        path: '/pages/news/news',

        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    } else {
      // 积分+1
      // 签到次数置零
      this.setData({
        qiandaoNum: 0
      })
      this.addJinbi()
      this.subQiandaoNum()

    }
  },
  // 积分+1
  // 签到次数置零
  // 添加积分
  addJinbi() {
    wx.cloud.database().collection('user').where({
        _openid: openid
      })
      .update({
        data: {
          jinbiNum: wx.cloud.database().command.inc(zhi)
        }
      })
  },
  // 签到次数置零
  subQiandaoNum() {
    wx.cloud.database().collection('lookvideos_qiandao').where({
      _openid:openid
    })
    .update({
      data: {
        qiandaoNum: 0
      }
    }).then(() => {
      wx.showToast({
        icon: 'none',
        title: `签到成功，积分+${zhi}`,
      })
    })
  },
  // 取消
  noQiandao() {
    wx.navigateBack({
      delta: 1,
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // title:  app.globalData.getGlobalShareMedia.jinbiShareText,
      // path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
      // imageUrl: app.globalData.getGlobalShareMedia.jinbiShareLgImg,
      title: `${app.globalData.userInfo.nickName}带你${app.globalData.SysController.jinbi_page_top_show.jinbiShareText}`,
      path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
      imageUrl: app.globalData.SysController.jinbi_page_top_show.jinbiShareLgImg,
    }
  },


})